package com.alibaba.dingtalk.openapi.helper;

import com.alibaba.dingtalk.openapi.exception.OApiException;
import com.alibaba.dingtalk.openapi.exception.OApiResultException;
import com.alibaba.dingtalk.openapi.utils.HttpHelper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 与钉钉客户系统打通相关接口，目前已停止调用
 * @author kaka
 */
public class CrmHelper {

	/**
	 * crm 获取客户的表单样式
	 * @param accessToken
	 * @param agentId
	 * @return
	 * @throws OApiException
	 */
	public static JSONObject customerGetForm(String accessToken, String agentId ) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/customer/get_form?" +
				"access_token=" + accessToken + "&agentId=" + agentId;
		JSONObject response = HttpHelper.httpGet(url);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException("获取客户表单数据时出错, response = null");
		}
	}
	
	/**
	 * crm 获取客户列表
	 * @param accessToken
	 * @param agentId
	 * @param offset
	 * @param size
	 * @return
	 * @throws OApiException
	 */
	public static JSONObject customerList(String accessToken, String agentId, Integer offset, Integer size) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/customer/list?" +
				"access_token=" + accessToken + "&agentId=" + agentId + "&offset=" + offset + "&size=" + size;
		JSONObject response = HttpHelper.httpGet(url);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException("获取客户列表数据时出错, response = null");
		}
	}
	
	/**
	 * crm 获取客户列表
	 * @param accessToken
	 * @param agentId
	 * @param pageHelper
	 * @return
	 * @throws OApiException
	 */
	public static JSONArray customerList(String accessToken, String agentId, DingTalkPageHelper pageHelper)
			throws OApiException {
		String url = Env.OAPI_HOST + "/crm/customer/list?" +
				"access_token=" + accessToken + "&agentId=" + agentId;
		if(pageHelper != null) {
			url = url + "&offset=" + pageHelper.getOffset() + "&size=" + pageHelper.getSize();
		} else {
			throw new OApiResultException("分页实体不存在");
		}
		JSONObject response = HttpHelper.httpGet(url);
		if (response.containsKey("customerList")) {
			JSONArray arr = response.getJSONArray("customerList");
			boolean hasMore = response.getBooleanValue("hasMore");
			//更新offset值
			pageHelper.setHasMore(hasMore);
			return arr;
		}
		else {
			throw new OApiResultException("customerList 接口获取数据时出错");
		}
	}
	
	/**
	 * 获取客户信息
	 * @param accessToken
	 * @param agentId
	 * @param customerId
	 * @return
	 * @throws OApiException
	 */
	public static JSONObject customerInfo(String accessToken, String agentId, String customerId) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/customer/info?" +
				"access_token=" + accessToken + "&customerId=" + customerId + "&agentId=" + agentId;
		JSONObject response = HttpHelper.httpGet(url);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException("获取客户详细数据时出错, response = null");
		}
	}

	/**
	 * 新增或更新客户的表单样式
	 * @param accessToken
	 * @param agentId
	 * @param schemaData
	 * @return
	 * @throws OApiException
	 */
	public static JSONObject customerAddOrUpdateForm(String accessToken, String agentId, JSONObject schemaData) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/customer/add_or_update_form?" +
				"access_token=" + accessToken;
		
		JSONObject args = new JSONObject();
		args.put("agentId", agentId);
		args.put("schemaData", schemaData);
		JSONObject response = HttpHelper.httpPost(url, args);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException("新增或更新客户的表单样式时出错, response = null");
		}
	}
	
	/**
	 * 新增或更新客户
	 * @param accessToken
	 * @param agentId
	 * @param formData
	 * @return
	 * @throws OApiException
	 */
	public static JSONObject customerAddOrUpdate(String accessToken, String agentId, JSONArray formData) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/customer/add_or_update?" +
				"access_token=" + accessToken;
		
		JSONObject args = new JSONObject();
		args.put("agentId", agentId);
		args.put("formData", formData);
		JSONObject response = HttpHelper.httpPost(url, args);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException("新增或更新客户时出错, response = null");
		}
	}
	
	
	/**
	 * crm  获取客户联系人的表单样式
	 * @param accessToken
	 * @param agentId
	 * @return
	 * @throws OApiException
	 */
	public static JSONObject contactGetForm(String accessToken, String agentId ) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/contact/get_form?" +
				"access_token=" + accessToken + "&agentId=" + agentId;
		JSONObject response = HttpHelper.httpGet(url);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException("获取联系人表单数据时出错, response = null");
		}
	}
	
	/**
	 * crm 获取客户联系人列表
	 * @param accessToken
	 * @param agentId
	 * @param offset
	 * @param size
	 * @return
	 * @throws OApiException
	 */
	public static JSONObject contactList(String accessToken, String agentId, String customerId, Integer offset, Integer size) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/contact/list?" +
				"access_token=" + accessToken + "&customerId=" + customerId + "&agentId=" + agentId + "&offset=" + offset + "&size=" + size;
		JSONObject response = HttpHelper.httpGet(url);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException("获取客户联系人表单数据时出错, response = null");
		}
	}
	
	/**
	 * crm 获取客户联系人列表
	 * @param accessToken
	 * @param agentId
	 * @param customerId
	 * @param pageHelper
	 * @return
	 * @throws OApiException
	 */
	public static JSONArray contactList(String accessToken, String agentId, String customerId, DingTalkPageHelper pageHelper) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/contact/list?" +
				"access_token=" + accessToken + "&customerId=" + customerId + "&agentId=" + agentId;
		if(pageHelper != null) {
			url = url + "&offset=" + pageHelper.getOffset() + "&size=" + pageHelper.getSize();
		} else {
			throw new OApiResultException("分页实体不存在");
		}
		JSONObject response = HttpHelper.httpGet(url);
		if (response.containsKey("crmContactList")) {//contactList
			JSONArray arr = response.getJSONArray("crmContactList");
			boolean hasMore = response.getBooleanValue("hasMore");
			//更新offset值
			pageHelper.setHasMore(hasMore);
			return arr;
		}
		else {
			throw new OApiResultException("customerList 接口获取数据时出错");
		}
	}
	
	/**
	 * crm 获取客户联系人信息
	 * @param accessToken
	 * @param agentId
	 * @param customerId
	 * @param contactId
	 * @return
	 * @throws OApiException
	 */
	public static JSONObject contactInfo(String accessToken, String agentId, String customerId, String contactId) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/contact/info?" +
				"access_token=" + accessToken + "&customerId=" + customerId  + "&contactId=" + contactId + "&agentId=" + agentId;
		JSONObject response = HttpHelper.httpGet(url);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException("获取客户联系人详情数据时出错, response = null");
		}
	}
	
	/**
	 * 新增或更新联系人的表单样式
	 * @param accessToken
	 * @param agentId
	 * @param schemaData
	 * @return
	 * @throws OApiException
	 */
	public static JSONObject contactAddOrUpdateForm(String accessToken, String agentId, JSONObject schemaData) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/contact/add_or_update_form?" +
				"access_token=" + accessToken;
		
		JSONObject args = new JSONObject();
		args.put("agentId", agentId);
		args.put("schemaData", schemaData);
		JSONObject response = HttpHelper.httpPost(url, args);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException("新增或更新联系人的表单样式时出错, response = null");
		}
	}
	
	/**
	 * 新增或更新客户联系人
	 * @param accessToken
	 * @param agentId
	 * @param customerId
	 * @param formData
	 * @return
	 * @throws OApiException
	 */
	public static JSONObject contactAddOrUpdate(String accessToken, String agentId, String customerId, JSONArray formData) throws OApiException {
		String url = Env.OAPI_HOST + "/crm/contact/add_or_update?" +
				"access_token=" + accessToken;
		
		JSONObject args = new JSONObject();
		args.put("agentId", agentId);
		args.put("customerId", customerId);
		args.put("formData", formData);
		JSONObject response = HttpHelper.httpPost(url, args);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException("新增或更新客户联系人出错, response = null");
		}
	}
	
}
